package com.aaa.controller.mht;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONUtil;
import com.aaa.controller.BaseController;
import com.aaa.entity.mht.MHTQuestion;
import com.aaa.entity.system.PageOrder;
import com.aaa.entity.Result;
import com.aaa.service.mht.MHTQuestionService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


/**
 * 问题表(QuestionMht)表控制层
 *
 * @author makejava
 * @since 2022-08-30 14:27:41
 */
@RestController
@RequestMapping("mht")
public class MHTQuestionController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private MHTQuestionService mhtQuestionService;

    @RequestMapping("query.do")
    public String queryAll(PageOrder pageOrder, MHTQuestion mhtQuestion) {
        Page page = mhtQuestionService.queryAll(pageOrder, mhtQuestion);
        JSONConfig jsonConfig = JSONConfig.create();
        jsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        String jsonStr = JSONUtil.toJsonStr(success(page.getTotal(), page.getResult()), jsonConfig);
        return jsonStr;
    }

    /**
     * 根据id删除数据
     * @param ids
     * @return
     */
    @RequestMapping("delete.do")
    public Result delete(int[] ids) {
        boolean b = mhtQuestionService.delete(ids);
        if (b) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @RequestMapping("insert.do")
    public Result insertUser(MHTQuestion mhtQuestion) {
        int insert = mhtQuestionService.insertMHT(mhtQuestion);
        if (insert > 0) {
            return success();
        } else {
            return fail();
        }
    }

    @RequestMapping("update.do")
    public Result updateUser(MHTQuestion mhtQuestion) {
        int updateUser = mhtQuestionService.updateMHT(mhtQuestion);
        if (updateUser > 0) {
            return success();
        } else {
            return fail();
        }
    }

    //用户查询页面
    @RequestMapping("toShowMHT.do")
    public ModelAndView toShowUser(ModelAndView modelAndView){
        modelAndView.setViewName("mht/showMHT");
        return modelAndView;
    }

    @RequestMapping("repetitionName.do")
    public Result repetitionName(MHTQuestion mhtQuestion){
        boolean b = mhtQuestionService.repetitionName(mhtQuestion);
        if (b){
            return success("题目名称可用");
        }
        return fail("题目名称重复，不可用");
    }

    /**
     * 查询题目类型
     * @return
     */
    @RequestMapping("queryDictionary.do")
    public Result queryDictionary(){
        List<MHTQuestion> typeList = mhtQuestionService.queryDictionary();
        if(ObjectUtil.isNotEmpty(typeList)){
            return success(typeList);
        }
        return fail();
    }


}

